package com.kwin;

import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestHash {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.flushDB();
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");

        // 添加名称为map的hash元素
        jedis.hmset("map",map);
        // 向名称为map的hash中添加key为key5,value为value5的元素
        jedis.hset("map","key5","value5");
        Map<String, String> ret = jedis.hgetAll("map");
        System.out.println("key为map的hash的所有键值对:" + ret);
        Set<String> keys = jedis.hkeys("map");
        System.out.println("key为map的hash的所有键:" + keys);
        List<String> values = jedis.hvals("map");
        System.out.println("key为map的hash的所有值:" + values);

        System.out.println("将key6保存的值加上一个整数，如果key6不存在则添加key6:" + jedis.hincrBy("map","key6",3));
        System.out.println("key为map的hash的所有键值对:" + jedis.hgetAll("map"));
        System.out.println("将key6保存的值加上一个整数，如果key6不存在则添加key6:" + jedis.hincrBy("map","key6",3));
        System.out.println("key为map的hash的所有键值对:" + jedis.hgetAll("map"));

        System.out.println("删除一个或多个键值对:" + jedis.hdel("map","key2","key3"));
        System.out.println("key为map的hash的所有键值对:" + jedis.hgetAll("map"));

        System.out.println("判断hash中是否存在key2:" + jedis.hexists("map","key2"));
        System.out.println("判断hash中是否存在key4:" + jedis.hexists("map","key4"));
        System.out.println("获取hash中的值:" + jedis.hmget("map","key4", "key5"));
        System.out.println("获取hash中的值:" + jedis.hget("map","key4"));

    }
}
